import Vue from 'vue'
const eventBus = new Vue({
  computed:{
    severStatus(){
    }
  }
})

var requireSocket = {
  channels: []
}

var requireBus = [
  {
    channel: 'exampleChannel',
    component: 'exampleComponent'
  }
]

eventBus.$on('Subscribe', reqMsg => {
  console.log('开始请求订阅频道：' + reqMsg.channels)
  dealBusSub(reqMsg)
  dealSocketSub(reqMsg)
})

eventBus.$on('reSubscribe', () => {
  console.log('重新订阅频道：')
  subscribeChannel()
})

eventBus.$on('Unsubscribe', reqMsg => {
  console.log('开始请求取消订阅频道：' + reqMsg.channels)
  dealBusUnsub(reqMsg)
  dealSocketUnsub(reqMsg)
})

function dealSocketUnsub(reqMsg) {
  reqMsg.channels.forEach(element => {
    if (checkSockUnsubIsNeed(element)) {
      //向服务器申请删除socket频道
      var temp = {
        channels: []
      }
      temp.channels.push(element)
      eventBus.$socket.emit('Unsubscribe', temp, respone => {
        console.log('成功取消订阅频道：' + element)
      })
    }
  })
}

function dealBusUnsub(reqMsg) {
  reqMsg.channels.forEach(element => {
    if (checkBusSubAlreadyExists(element, reqMsg.component)) {
      console.log(reqMsg.component + '页面的' + element + '频道在bus中已经找到，开始准备取消订阅')
      //在这里要删除bus中的频道
      for (let index = 0; index < requireBus.length; index++) {
        const elementbus = requireBus[index]
        if (elementbus.channel === element && elementbus.component === reqMsg.component) {
          //index 当前元素索引；1：需要删除的元素个数
          requireBus.splice(index, 1)
          console.log(reqMsg.component + '页面的' + element + '频道在bus中取消订阅成功！')
          break
        }
      }
      // requireBus = requireBus.map((item, index) => {
      //   debugger

      // })
    } else {
      console.log(reqMsg.component + '页面的' + element + '频道在bus中没有找到')
    }
  })
}

//判断是否要向socket申请取消
function checkSockUnsubIsNeed(channel) {
  //检查requireBus，看看有没有别的页面订阅这个频道，如果有，则不向socket申请取消，如果没有，则申请取消
  requireBus.forEach(eleBus => {
    if (eleBus.channel === channel) {
      return false
    }
  })
  return true
}

//处理bus订阅的事情
function dealBusSub(reqMsg) {
  reqMsg.channels.forEach(element => {
    if (checkBusSubAlreadyExists(element, reqMsg.component)) {
      console.log(reqMsg.component + '页面的' + element + '频道在bus已经订阅过了')
    } else {
      var temp = {}
      temp.channel = element
      temp.component = reqMsg.component
      requireBus.push(temp)
    }
  })
}

//检查该订阅请求是否已经存在bus中
function checkBusSubAlreadyExists(sub, component) {
  for (let index = 0; index < requireBus.length; index++) {
    const element = requireBus[index]
    if (element.channel === sub && element.component === component) {
      return true
    }
  }
  return false
}

//处理socket订阅的事情
function dealSocketSub(reqMsg) {
  reqMsg.channels.forEach(element => {
    if (checkSocketSubAlreadyExists(element)) {
      //socket订阅了该channel ，则什么都不做
      console.log(element + '频道已经订阅过了，可以直接开始接收数据')
    } else {
      //socket没有订阅了该channel ，马上去订阅
      requireSocket.channels.push(element)
    }
  })
  subscribeChannel()
}

//检查该订阅请求是否已经存在bus中，是否有必要向后台socket订阅频道
function checkSocketSubAlreadyExists(str) {
  var index = requireSocket.channels.indexOf(str)
  // -1 说明array中不存在id为str的对象
  if (index === -1) {
    return false
  } else {
    return true
  }
}

function subscribeChannel() {
  eventBus.$socket.emit('Subscribe', requireSocket, response => {
    console.log(requireSocket);
    console.log('成功订阅频道：' + requireSocket.channels)
  })
}

export default eventBus

